@page "/Blog/Collection"
@using LinCms.Entities.Blog

<PageTitle>收藏集</PageTitle>

<blockquote class="quote-info m-3 p-0 pl-2"><h5>提示：这个页面使用 AdminBlazor 生成器的示例。【菜单管理】->【增删改查】</h5></blockquote>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">

            <AdminTable2 TItem="Collection" Context="item" PageSize="20" Title="收藏集"TableTd99Width="230"
                OnQuery="OnQuery" InitQuery="InitQuery" OnEdit="OnEdit">

                <TableHeader>
                    <th>名称</th>
                    <th>描述</th>
                    <th>收藏数量</th>
                    <th>类型</th>
                    <th>创建时间</th>
                </TableHeader>
                <TableRow>
                    <td>@item.Name</td>
                    <td>@item.Remark</td>
                    <td>@item.Quantity</td>
                    <td>@item.PrivacyType</td>
                    <td>@item?.CreatedTime?.ToString("yyyy-MM-dd HH:mm:ss")</td>
                </TableRow>
                <TableTd99>
                    <button @onclick="e => BeginAllocArticles(item)" type="button" class="mr-2 btn btn-light btn-xs"><i class="fa fa-user-secret"></i>分配随笔</button>
                </TableTd99>
                <EditTemplate>
                    <div class="row form-inline form-inline-end">
                        <div class="form-group col-12">
                            <label class="form-label">名称</label>
                            <input @bind="item.Name" type="text" class="form-control" placeholder="" maxlength="50">
                        </div>
                        <div class="form-group col-12">
                            <label class="form-label">描述</label>
                            <textarea @bind="item.Remark" class="form-control" placeholder="" maxlength="500"></textarea>
                        </div>
                        <div class="form-group col-12">
                            <label class="form-label">收藏数量</label>
                            <input @bind="item.Quantity" type="number" class="form-control" placeholder="">
                        </div>
                        <div class="form-group col-12">
                            <label class="form-label">类型</label>
                            <SelectEnum TEnum="PrivacyType" @bind-Value="item.PrivacyType" />
                        </div>
                    </div>
                </EditTemplate> 
            </AdminTable2>
        </div>
    </div>
</div>

<AllocTable2 TItem="Collection" TChild="Article" @bind-Item="@allocItemArticles" ChildProperty="Articles" Title="@($"【分配随笔】{allocItemArticles?.Name}")">
    <TableHeader>
        <th>随笔标题</th>
    </TableHeader>
    <TableRow>
        <td>@context.Title</td>
    </TableRow>
</AllocTable2>

@code {

    [Inject] IAggregateRootRepository<Collection> repo { get; set; }

    async Task InitQuery(AdminQueryInfo e)
    {
        e.Filters = new AdminFilterInfo[]
        {
            new AdminFilterInfo("类型", "PrivacyType", "公开可见,仅自己可见", "0,1"),
        };
        await Task.Yield();
    }
    void OnQuery(AdminQueryEventArgs<Collection> e)
    {
        e.Select.WhereIf(e.Filters[0].HasValue, a => a.PrivacyType == e.Filters[0].Value<PrivacyType>())
            .WhereIf(!e.SearchText.IsNull(), a => a.Name.Contains(e.SearchText));
    }

    async Task OnEdit(Collection item)
    {
        await Task.Yield();
    }

    Collection allocItemArticles;
    [AdminButton("alloc_articles")]
    void BeginAllocArticles(Collection item) => allocItemArticles = item;
}
